home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / N-P / OOP for C.sit / OIC.ƒ / object.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-09  |  2.4 KB  |  147 lines  |  [TEXT/KAHL]

  1. /*
  2.  *        Object - the root class, inherited eventually by all classes.
  3.  *
  4.  *            Copyright © John Wainwright 1988
  5.  *
  6.  *    Supers : None.
  7.  *
  8.  *    Class Vars : None
  9.  *
  10.  *    Class Methods : None
  11.  *
  12.  *    Methods :
  13.  *
  14.  *        new            - dummy new.
  15.  *        className    - returns class name String
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include "oic.h"
  20. #include "generics.h"
  21.  
  22. class Object;                        /* root Object class                 */
  23.  
  24. /* -------------------- Object Instance methods ---------------------------- */
  25.  
  26. static object
  27. _new(self)
  28.     object    self;
  29. {
  30.     return self;
  31. }
  32.  
  33. static object
  34. _className(self)
  35.     object    self;
  36. {
  37.     return New(String, ClassOf(self)->c_name);
  38. }
  39.  
  40. static void
  41. _dispose(self)
  42.     object    self;
  43. {
  44.     free(self);
  45. }
  46.  
  47. static object
  48. _print(self)
  49.     object    self;
  50. {
  51.     object replist = repList(self);
  52.     
  53.     print(replist);
  54.     deepDispose(replist);
  55. }
  56.  
  57. static object
  58. _repList(self)
  59.     object    self;
  60. {
  61.     register object    p;
  62.     register int    i, l;
  63.     object            replist;
  64.     char            buf[100];
  65.     
  66.     replist = New(Replist, "<", ">", " ");
  67.     sprintf(buf, "%s:", ClassNameOf(self));
  68.     append(replist, New(String, buf));
  69.     l = ClassOf(self)->c_allocz / sizeof(object);
  70.     for (p = self + 1, i = 1; i < l; i++, p++)
  71.         if (IsObj(*p))
  72.             append(replist, repList(*p));
  73.         else
  74.         {
  75.             sprintf(buf, "%8.8lx", *p);
  76.             append(replist, New(String, buf));
  77.         }
  78.     return replist;
  79. }
  80.  
  81. static object
  82. _cantDo(self)
  83.     object    self;
  84. {
  85.     fprintf(stderr, "cant do this method\n");
  86.     return END;
  87. }
  88.  
  89. static void
  90. _map(self, dummy, f)
  91.     object    self;
  92.     char    *dummy;
  93.     void    (**f)();
  94. {
  95.     object seq, item;
  96.     
  97.     for (seq = sequence(self); item = next(seq); )
  98.         (**f)(item);
  99. }
  100.  
  101. static object
  102. _allInstances()
  103. {
  104.     return New(List, END);
  105. }
  106.  
  107. static object
  108. _deepInstances()
  109. {
  110.     return New(List, END);
  111. }
  112.  
  113. static int
  114. _eq(self, ivs, other)
  115.     object    self;
  116.     char    *ivs;
  117.     object    *other;
  118. {
  119.     return (ClassOf(self) == ClassOf(*other) &&
  120.             ClassOf(self)->c_allocz == ClassOf(*other)->c_allocz &&
  121.             strncmp(self, *other, ClassOf(self)->c_allocz));
  122. }
  123.  
  124. /* ------------------- Init the Object class ------------------------------- */
  125.  
  126. /*        
  127.  *        note that the Object has already been made in the ONLY function
  128.  *        that should call this: _InitRootClasses in obj.c
  129.  */
  130.  
  131. _InitObject()
  132. {
  133.     AddMethods(Object,
  134.         eqGeneric,                _eq,
  135.         newGeneric,             _new,
  136.         classNameGeneric,         _className,
  137.         printGeneric,            _print,
  138.         repListGeneric,            _repList,
  139.         disposeGeneric,            _dispose,
  140.         cantDoGeneric,            _cantDo,
  141.         mapGeneric,                _map,
  142.         allInstancesGeneric,     _allInstances,
  143.         deepInstancesGeneric,    _deepInstances,
  144.         END);
  145. }
  146.  
  147.